/**
 * 
 */
package neptune.web.server.repository;

import java.io.File;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import it.robertobifulco.ias.RepositoryException;
import it.robertobifulco.ias.rolemanager.RoleRepository;
import it.robertobifulco.ias.rolemanager.RoleUndefinedException;

/**
 * @author Roberto Bifulco [RobertoBifulco.it] [info@robertoBifulco.it]
 * 
 */
public class XMLRoleRepository implements RoleRepository {

	private String policyFilePath = "policy.xml";
	private File file;
	private DocumentBuilder builder;

	/**
	 * @throws ParserConfigurationException
	 * @throws IOException
	 * @throws SAXException
	 * 
	 */
	public XMLRoleRepository(String policyFilePath) throws RepositoryException {
		try {
			this.policyFilePath = policyFilePath;
			DocumentBuilderFactory factory = DocumentBuilderFactory
					.newInstance();
			this.builder = factory.newDocumentBuilder();

			this.file = new File(this.policyFilePath);
		} catch (ParserConfigurationException e) {
			throw new RepositoryException(e);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * wms.ias.rolemanager.RoleRepository#getRoleOperations(java.lang.String)
	 */
	public synchronized String[] getRoleOperations(String roleName)
			throws RoleUndefinedException {
		try {
			Document document = builder.parse(this.file);

			NodeList nodes = document.getElementsByTagName("name");

			String[] operations;
			Node node;

			for (int i = 0; i < nodes.getLength(); ++i)
				if (nodes.item(i).getTextContent().equals(roleName)) {
					node = nodes.item(i).getNextSibling().getNextSibling();
					NodeList operationNodes = node.getChildNodes();
					operations = new String[operationNodes.getLength() / 2];
					for (int j = 1; j < operationNodes.getLength(); j = j + 2)
						operations[(j - 1) / 2] = operationNodes.item(j)
								.getTextContent();
					return operations;
				}

			throw new RoleUndefinedException("The role " + roleName
					+ " is undefined");

		} catch (SAXException e) {
			throw new RoleUndefinedException(e);
		} catch (IOException e) {
			throw new RoleUndefinedException(e);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see wms.ias.rolemanager.RoleRepository#getRoles()
	 */
	public synchronized String[] getRoles() {
		try {
			Document document = builder.parse(this.file);

			NodeList nodes = document.getElementsByTagName("name");

			String[] roles = new String[nodes.getLength()];
			for (int i = 0; i < nodes.getLength(); ++i) {
				roles[i] = nodes.item(i).getTextContent();
			}

			return roles;
		} catch (SAXException e) {
			throw new RuntimeException(e);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

}
